package cn.clx.org.controller;

import cn.clx.basic.exception.BusinessException;
import cn.clx.basic.util.AjaxResult;
import cn.clx.basic.util.ExcelUtil;
import cn.clx.basic.util.PageList;
import cn.clx.org.domain.Shop;
import cn.clx.org.domain.ShopAuditLog;
import cn.clx.org.mapper.ShopMapper;
import cn.clx.org.query.ShopQuery;
import cn.clx.org.service.IShopAuditLogService;
import cn.clx.org.service.IShopService;
import cn.clx.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    @Autowired
    public IShopAuditLogService shopAuditLogService;


    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.removeById(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }


    /**
     *
     *
     * @return
     */
    @GetMapping
    public List<Shop> list() {
        return shopService.queryAll();
    }

    /**
     * 得到所有的店铺信息
     * @param query
     * @return
     */
    @PostMapping("/list")
    public PageList<Shop> ShopList(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }


    /**
     * 商家入驻
     *
     * @param
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult ShopLogin(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统出错了，正在殴打程序员");
        }

    }

    /**
     * 驳回重提交
     * @param shop
     * @return
     */
    @PostMapping("/turnDown")
    public AjaxResult turnDown(@RequestBody Shop shop) {
        try {
            shopService.turnDown(shop);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统出错了，正在殴打程序员");
        }

    }

    /**
     * 审核通过
     *
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopAuditLogService.examinationPassed(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统出错了，正在殴打程序员");
        }


    }

    /**
     * 驳回审核
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopAuditLogService.reject(shopAuditLog);
            return AjaxResult.getAjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统出错了，正在殴打程序员");
        }

    }

    /**
     * 导出shop表中的数据为Excel
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            /**
             * 第一个参数是要导出的数据list  第二个是标题   第三个是Excel表中sheet的名称  Shop.class 映射domain中的那个表 , 导出的文件名
             * response 响应
             */
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * shop导入Excel表
     * @param
     * @return
     */
    @PostMapping("/importExcel")
    public void importExce(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        shopService.saveList(list);
    }

    /**
     * 店长激活接口
     */
    @GetMapping("/activation/{id}")
    public String activation(@PathVariable("id") Long id){
        try {
            Shop shop = shopService.queryById(id);
            shop.setState(2);
            shopService.update(shop);
            return "您已激活，可以开始开始接单了";
        } catch (Exception e) {
            e.printStackTrace();
            return "激活失败，请联系客服";
        }
    }

    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.queryEcharts();
        List<String> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }



}
